﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqAsset
	{
        public List<Asset> GetAssets(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<AssetEntity> query = db.AssetEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "AssetID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.AssetID);
                            else
                                query = query.OrderByDescending(c => c.AssetID);
                            break;
                        case "AssetName":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.AssetName);
                            else
                                query = query.OrderByDescending(c => c.AssetName);
                            break;
                       
                    }
                }

                return query.Select(c => Mapper.ToAssetObject(c)).ToList();
            }
        }


        public Asset GetAsset(int assetId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToAssetObject(db.AssetEntities
                            .SingleOrDefault(p => p.AssetID == assetId));
            }
        }


        public Asset GetAssetForCustomer(string customerId)
        {

            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                Asset ast = new Asset();

                var asset = (from ass in db.AssetEntities
                               where ass.CustomerID == customerId
                               group ass by new { ass.CustomerID } into g
                               select new Asset
                               {
                                   AssetId = g.FirstOrDefault().AssetID,
                                   CustomerId = g.FirstOrDefault().CustomerID,
                                   FixedDepo = (float)g.Sum(p => (double?)p.FixedDepo).GetValueOrDefault(),
                                   Saving = (float)g.Sum(p => (double?)p.Savings).GetValueOrDefault(),
                                   OthCeyIns = (float)g.Sum(p => (double?)p.OthCeyInsu).GetValueOrDefault(),
                                   OthIns = (float)g.Sum(p => (double?)p.OthInsu).GetValueOrDefault(),
                                   Others = (float)g.Sum(p => (double?)p.Others).GetValueOrDefault(),
                                   
                               });

                if (asset.FirstOrDefault() != null)
                {
                    ast.FixedDepo = asset.FirstOrDefault().FixedDepo;
                    ast.Saving = asset.FirstOrDefault().Saving;
                    ast.OthCeyIns = asset.FirstOrDefault().OthCeyIns;
                    ast.OthIns = asset.FirstOrDefault().OthIns;
                    ast.Others = asset.FirstOrDefault().Others;

                }
                else
                {
                    ast.FixedDepo = 0;
                    ast.Saving = 0;
                    ast.OthCeyIns = 0;
                    ast.OthIns = 0;
                    ast.Others = 0;
                }

                return ast;

            }




            //using (SODataDataContext db = DataContextFactory.CreateContext())
            //{


            //    IQueryable<AssetEntity> asset = from ass in db.AssetEntities
            //                                    where ass.CustomerID == customerId
            //                                    select ass;


            //    return asset.Select(c => Mapper.ToAssetObject(c)).ToList();

            //}

        }


        public void InsertAsset(Asset asset)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    AssetEntity entity = Mapper.ToAssetEntity(asset);
                    db.AssetEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    asset.AssetId= entity.AssetID;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to Insert contact " + asset.AssetName);
                }
            }
        }


        public int UpdateAsset(Asset asset)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    AssetEntity entity = Mapper.ToAssetEntity(asset);
                    db.AssetEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        public int DeleteGroup(Asset asset)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    AssetEntity entity = Mapper.ToAssetEntity(asset);
                    db.AssetEntities.Attach(entity, false);
                    db.AssetEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to contact record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


	}
}
